
String.prototype.format = function () {
  var args = arguments;
  return this.replace(/\{(\d+)\}/g, function (m, n) { return args[n]; });
};
var REMOVE_CHS = true;
var NAS_DOWN_URL = "http://snhome.mooo.com:88/download/addfile.php?url={0}&file_name={1}&confirm=true";





//override the fill_bt_list from task.js
//alert('hiw');
function fill_bt_list(record){

	TS.hideNow();
	var o = record.Result.Record;
	var len = o.length;
	if( len == 0 ){
		cancle_task();
		var req = INTERFACE_URL+"/fill_bt_list?callback=edit_bt_list&tid="+record.Result.Tid+"&infoid="+record.Result.Infoid+"&uid="+G_USERID+"&ed=1";
		var jsonReq = new JSONscriptRequest(req);
		jsonReq.buildScriptTag();
		jsonReq.addScriptTag();
		return;
	}
	//var linkid = "durl"+record.Result.Tid.toString();
	var bt_title = _$('taskname'+record.Result.Tid).value;
	var data={list:[],ftitle:bt_title};
	

	for( var i = 0; i < len; i++ ){
		if( o[i].taskid == 0 ) continue;
		var tmp=o[i];
		tmp.check_str = "";
		if( o[i].downurl == "" ) {
			tmp.check_str = "checked='checked'";
		}
		tmp.i=i.toString();
		tmp.bg = (i%2==0)?0:1;
		
		tmp.showlivetime=0;
		if( o[i].download_status == "2" || o[i].download_status == "3" || o[i].download_status == "5" ) {
			tmp.showlivetime = 1;
		}

		tmp.isvod = 0;
		if(typeof(g_isfree)!='undefined' && g_isfree && o[i].download_status == "2" && o[i].downurl==''){
			o[i].downurl = 'h';
		}
		if( o[i].downurl != "" ) {
			var tname = o[i].title;
			var vurl = "";
			var tx = tname.lastIndexOf(".");
			var suffix = "";
			if( tx != -1 )
			suffix = tname.substr(tx+1).toLowerCase();
			if( o[i].vod == 1 )	{
				var vhttp = "http://vod.";
				var vix = o[i].downurl.indexOf(".");
				if( vix != -1 )
				vurl = o[i].downurl.substr(vix+1);
				vurl = "http://cloud.vip.xunlei.com/play.html?url="+vhttp+vurl+"&cid="+o[i].cid+"&moviename="+o[i].title;
				var vurl2 = o[i].downurl+"&cid="+o[i].cid+"&moviename="+o[i].title;

				tmp.vurl2=vurl2;
				tmp._status=1;
			}
			var vod_str = "&nbsp;";
			if( vurl != "" ) {
				tmp.isvod=1;
			}
		}else if( o[i].download_status == 3 ){ // o[i].livetime == "-1"
			tmp._status=0;
		}else {
		}
		
		var cdns=[];
		for(var j=0;j<o[i].cdn.length;j++){
			cdns.push({
				i:i.toString(),
				j:j.toString(),
				v:o[i].cdn[j]
			});
			
		}
		tmp.cdns = cdns;
		data.list.push(tmp);
	}

	var _timer=0;
	data.infoid = record.Result.Infoid;
	data.taskid = record.Result.Tid;
	$('#show_bt_list').tpl('view_bt_list_tpl',data)
						.find('div[name=rw_list]')
						.hover(function(){
							if(!$(this).find('input[name=bt_list_ck]').attr('disabled'))
								$(this).css('cursor','pointer').addClass('rw_hoverbg');//cursor:pointer;
							else
								$(this).css('cursor','text');
						},function(){
							$(this).removeClass('rw_hoverbg');//cursor:pointer;
						})
						.click(function(e,a){
							$('#bt_view_list').hide();

							if($(this).find('input[name=bt_list_ck]').attr('disabled')) return false;
							var that = $(this);
							if(!$(e.target).hasClass('link_yunic')){
								that.find('div.p_yunbtn').hide();
							}
							var id = $(this).attr('i').toString();
							$('#show_bt_list .rw_list[i!='+id+'] .rwset').hide();
							$('#show_bt_list .rw_list[i!='+id+']').removeClass('rw_bg');
							if(!that.hasClass('rw_bg')){
								that.addClass('rw_bg');
							}
							

							var that = $(this);
							//_timer = setTimeout(function(){
								if(!a){
									that.find('div.rwset').show();
									//if(!g_playopen){
										//that.find('a.ic_play').hide();
									//}
									var openformat=that.attr('openformat');
									if(openformat=='rar' && !show_arc){
										that.find('a.ic_open').hide();
									}
									if(openformat=='music' && (!g_musicplayopen /*|| !$.browser.msie*/)){
										that.find('a.ic_play').hide();
									}
									
								}

								input_change('input[name=bt_list_ck]');
							//},100);
							e.stopPropagation();
							return false;
						})
						.bind('click',function(){
							return;
							/*return;
							if($(this).find('input[type=checkbox]')[0].disabled) return;
							$(this).find('input[type=checkbox]')[0].checked = !$(this).find('input[type=checkbox]')[0].checked;
							$($(this).find('input[type=checkbox]')[0]).triggerHandler('click');
							*/

							var that = $(this);
							var id=that.attr('i').toString();
							$('#show_bt_list').find('input[type=checkbox][_i!='+id+']').each(function(){
								if($(this).data('from')==1){
									$(this).attr({checked:false});	
								}
							});
							if(!$(this).find('input[type=checkbox]')[0].checked){
								$(this).find('input[type=checkbox]')[0].checked = true;
								$($(this).find('input[type=checkbox]')[0]).triggerHandler('click',1);
							}

						})
						.bind('contextmenu',function(e){
							$(this).click();
							if($(e.target).attr('showoncontextmenu')==1) return true;
							var that = $(this);
							if(that.attr('download_status')!=2){
								return false;
							}
							var rt = $('#bt_view_list').show();
							try{
								var top = e.clientY + $(document).scrollTop();
								rt[0].style.top=top.toString()+'px';
								rt[0].style.left=e.clientX.toString()+'px';
							}
							catch(e){
								rt.offset({top:e.clientY,left:e.clientX});
							}
							if((top+rt.height())>(document.documentElement.scrollTop+document.documentElement.clientHeight)){
								rt[0].style.top=(top - rt.height()).toString()+'px';
							}

							rt.find('a').unbind('click');

							rt.find('#bt_view_list_down').bind('click',function(){
								thunder_download(that.attr('i'),1);
							});


							if(that.attr('isvod')==1){
								rt.find('#bt_view_list_play').bind('click',function(){
									that.find('a.ic04').click();
								}).show().parent('p').show();
								rt.find('#bt_view_list_cloud').bind('click',function(){
									cloud_transformat([that.attr("i")], 1);
									send_stats2("http://lixian.xunlei.com/func/download/tool/cloud2");
								}).show().parent('p').show();			//add by guoyu
							}
							else{
								//rt.find('#bt_view_list_play').parent('p').hide();
								rt.find('#bt_view_list_cloud').parent('p').hide();	//add by guoyu
							}

							rt.find('#bt_view_list_email').bind('click',function(){
								send_to_fileemail(that.attr('i'),1);
							});

							rt.find('#bt_view_list_pan').bind('click',function(){
								save_to_pan(that.attr('i'),1);
							});

							rt.find('#bt_view_list_save').bind('click',function(){
								rm_saveas_bt(that.attr('i'));
							});

							//add by Steven -start
							rt.find('#bt_view_list_down_by_nas').bind('click',function(){
								//thunder_download(that.attr('i'),1);
								rm_saveas_nas(that.attr('i'));
							});
							//add by Steven -end
							
							var openformat = $('#btopenformat'+that.attr('i').toString()).val();
							if((openformat=='rar' && show_arc) || openformat=='music' || openformat=='image' || (openformat=='movie')) {
								$('#bt_view_list_open').unbind('click').bind('click',function(){
									var taskid = $('#bttaskid'+that.attr('i').toString()).val();
									open_task(that.attr('i'),openformat,1);
									return false;
								}).html('??').parent('p').show();

								if(openformat=='music'){
									$('#bt_view_list_open').html('??');
								}
								if(openformat=='movie'){
									$('#bt_view_list_open').html('???');
								}
								if(openformat=='music' && (!g_musicplayopen || !$.browser.msie)){
									$('#bt_view_list_open').parent('p').hide();
								}
								
							}
							else{
								$('#bt_view_list_open').parent('p').hide();
							}
		

							return false;
						})
						.find('input[name=bt_list_ck]').bind('click',function(event,from){
							/*if(from!=1 && $('.rw_list[i='+$(this).val().toString()+']').hasClass('rw_hoverbg')){
								if(!this.checked){
									clean(this.value);
								}
							}
							
							$(this).data('from',from==1?1:0);*/
							event.stopPropagation();
							function clean(id){
								var id=id.toString();
								$('.rw_list[i='+id+']').removeClass('rw_hoverbg').removeClass('rw_bg');
								$('.rw_list[i='+id+'] .rwset').hide();
							}
						})
						.change(function(){
							var i = $(this).attr('_i').toString();
							if(this.checked){
								$("#rw_list_"+i).removeClass('rw_bg').addClass("rw_bg");
							}
							else{ //input_change
								if($("#rw_list_"+i).find('.rwset').css('display')=='none'){
									$("#rw_list_"+i).removeClass('rw_bg');
								}
							}
						});

	pager.init({
							box:'#bt_page_info',
							tpl:'bt_pager_tpl',
							perpage:record.Result.btpernum,
							current:record.Result.now_page,
							totalnum:record.Result.btnum
						}).show({cid:record.Result.Infoid,taskid:record.Result.Tid});
	
	$('#show_bt_list').find("a[btdown=1]").bind('click',function(){
		thunder_download($(this).attr('i'),1);
		return false;
	});

	$('#show_bt_list').find('a[showoncontextmenu=1]').bind('contextmenu',function(event){
		if(g_isfree) return false;
		http_url_relocate1($(this).attr('i'),1);
		//event.stopPropagation();
	//	return true;
	});
	$('#view_bt_list_nav').tpl('view_bt_list_nav_tpl',{infoid:record.Result.Infoid,taskid:record.Result.Tid});
	panel.show('view_bt');

	$('#bt_view_all_input,#show_bt_list .sellection a').click(function(event){
		event.stopPropagation();
	});

	bt_view_nav();
	
	
	window.scroll(0,0);
	record = null;
	/*$('#view_bt_list_nav_cloud').bind('click',function(){
		C.list();
		return false;
	});*/
	resize_tab();

	if($.browser.msie && $.browser.version=='6.0'){
		//$.getScript('http://cloud.vip.xunlei.com/160/js/transparent.js?'+new Date());
	}
	
	return;
}




function rm_saveas_nas(id) {
	
	if(arguments[arguments.length-1]==='download'){
	}
	else {
		download_check(rm_saveas_nas,arguments);
		return;
	}
	//add By Steven - start
	var dl_urlid = "btdownurl"+id.toString();
	var dl_url = _$(dl_urlid).value;
	var durl = "btname"+id.toString();
	var dl_name = _$(durl).title.trim();
	//if(REMOVE_CHS) dl_name = dl_name.replace(/[\u4e00-\u9fa5]+/g, '');
	window.open(NAS_DOWN_URL.format(encodeURIComponent(dl_url),encodeURIComponent(dl_name)));
	//add By Steven - end
}

function _more_operation_lx(tid,t){
var bt = "btlist"+tid.toString();

if(t == 1){
var urlid = "dl_url"+tid.toString();
var fileurl = _$(urlid).value;
var dl_name = $('#durl'+tid).val();
//if(REMOVE_CHS) dl_name = dl_name.replace(/[\u4e00-\u9fa5]+/g, '');

var data={
title:'????',
desc:'?????????(?????????)',
url:fileurl
};
$('#copyurl_box').tpl('copyurl_box_tpl',data).show().pop();
$('#copyurl_box').find('textarea')[0].select();
window.open(NAS_DOWN_URL.format(encodeURIComponent(fileurl),encodeURIComponent(dl_name)));

return;
}
} 
